// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

function pic_replace(dom1,dom2){
    var file = document.getElementById(dom1).files;
    var reader = new FileReader();
    reader.readAsDataURL(file[0]);
    reader.onload = function(e){
        var Img = '<img class="act_img_sty" src="'+this.result+'" width="85px" height="100px"/>';
        $('.'+dom2).html(Img);
    }
}

$(document).ready(function () {
    // $("#banner").addClass("active");
    // $("#banner1").addClass("active");

    getData1(1);
    var len;
    function pageDivd(curr,page_num){
        layui.use(['laypage', 'layer'], function(){
            var laypage = layui.laypage
                ,layer = layui.layer;
            laypage.render({
                elem: 'demo7',
                count: page_num,
                limit:5,
                curr:curr,
                jump: function(obj,first){
                    if(!first){
                        getData1(obj.curr);

                    }
                }
            });
        })
    }
    function getData1(curr){
        var keyword = $("#search").val();
        $.ajax({
            type:"get",
            url:"http://114.116.72.220:9999/ShowIndexSwiper/",
            async:true,
            data:{
                page:curr ,
                row: 5,
                keyword:keyword
            },
            success:function(data){
                console.log(data);
                len = data.length;
                if(data.length==0){
                    layer.msg('暂无数据');
                }
                TableView(data);
                pageDivd(curr,data[1]);
            }
        });
    }
    function TableView(data){
        $('#group_table').html('');
        for (var i=0;i<data.length;i++) {
            var trText = `<tr num="${data[i].id}" class="${i%2 ? '' : 'tr_back'}">
		      		    
		      		    		<td>${i+1}</td>
		      		    		<td>
		      		    		  <img class="bannerImg" src="${data[i].img}" alt="">
		      		    		</td>
		      		    		<td>
		      		    			<button class="btn del" >删除</button>
		      		    		</td>
                        </tr>
		      		    	`;

            $('#group_table').append(trText) ;

        }

        $('.del').click(function () {
            var id = $(this).parent().parent().attr('num');
            console.log(id);
            $.ajax({
                type:"get",
                url:"http://114.116.72.220:9999/DeleteIndexSwiper/",
                async:true,
                data:{
                    swiperId:id
                },
                success:function(data){
                    console.log(data);
                    if(data == 'success'){
                        layer.msg('删除成功！',{time:1000},function () {
                            location.reload();
                        })
                    }
                }
            });
        });

        $('.confirm').click(function () {
            len++;
            console.log(len);
            if(len > 5){
                layer.msg('最多只能添加五张轮播图照片');
                return;
            }
            console.log('确定');
            if($('.banner').text() == '+'){
                layer.msg('请添加照片');
                return;
            }
            var formData = new FormData(document.getElementById('bannerImg'));
            // console.log(banner)
            $.ajax({
                type:'POST',
                url:'http://114.116.72.220:9999/AddIndexSwiper/',
                async:true,
                data:formData,
                processData: false,
                contentType: false,
                success:data=>{
                    console.log(data);
                    if(data == 'success'){
                        layer.msg('提交成功！',{time:500},function () {
                            len++;
                            location.reload();
                        })
                    }else{
                        layer.msg('提交失败！')
                    }
                }
            })
        })



        $('#search').blur(function () {
            if($('#search').val()==''){
                getData1(1)
            }
        })
        $('.searchPic').click(function () {
            getData1(1)
        })

    }
});

